// manager/pages/trialapproval/index.js
import { getSystemInfo } from '../../../utils/getSystemInfo';
import { request } from '../../../utils/request';
import util from '../../../common/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dict: {},
        params: {
            search: "",
            phone: "",
        },
        scroller: {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
        },
        show: false,
        actions: [],
        phone: "",

        // 审批意见
        showOpinion: false,
        opinionForm: {
            id: "",
            expName: "",
            expPhone: "",
            expDept: "",
            state: "",
            remark: "",
        },
        currentItem: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // this.setData({
        //     params: Object.assign(this.data.params,options)
        // })
        // this.getUserInfo();
        this.getDictionaryInfo('ecard_experience_sts');
        this.getExperienceUserList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
    // 获取当前登录者的用户信息
    getUserInfo(e) {        
        request({
            url: "/user/getUserInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {}
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    userInfo: res.data.userInfo
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /** 获取字典 */
    getDictionaryInfo(dictType) {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
                dictType
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    ['dict.'+dictType]: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /**
     * 重置数据
     */
    resetData(){
		this.resetParams();
		this.resetScroller();
    },
    /**
     * 重置下拉
     */
    resetScroller(){
        let scroller = {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
		}
		this.setData({
            scroller,
		})
    },

    refresh() {
        this.resetScroller()
        this.getExperienceUserList()
    },
    
	loadMore() {
        console.log("loadMore")
		this.getExperienceUserList()
    },
    /** 用户申请体验数据列表 */
    getExperienceUserList() {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/experienceUser/getExperienceUserList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                ...this.data.params,
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.creationTime = util.formatTime(new Date(item.creationTime));
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /**
     * 输入框输入时
     */
    input(e){
        let key = e.currentTarget.dataset.key;
        this.setData({
            ['params.'+key]: e.detail.value,
        })
    },
    // 搜索
    searchConfirm(e){
        console.log(e.detail.value)
        this.setData({
            'params.search': e.detail.value
        })
        this.getExperienceUserList();
    },
    searchBtn(e){
        this.getExperienceUserList();
    },
    /**
     * 拨打电话
     */
    makePhoneCall(e) {
        console.log(e)
        let { phone } = e.currentTarget.dataset;
        if(!phone){
            wx.showToast({
                title: "暂无联系方式",
                icon: 'none',
                duration: 2000
            })
            return
        }
        getSystemInfo({
            android: () => {
                let actions = [
                    { name: phone, color: '#999', disabled: true },
                    { name: '呼叫', color: '#000' },
                ]
                this.setData({
                    show: true,
                    phone: phone,
                    actions
                })
            },
            ios: () => {
                wx.makePhoneCall({
                    phoneNumber: phone
                })
            }
        })
    },
    onSelect(e) {
        console.log(e)
        this.setData({
            show: false
        })
        wx.makePhoneCall({
            phoneNumber: this.data.phone
        })
    },
    onCancel() {
        this.setData({ show: false });
    },
    /**
     * 跳转详情
     */
    detail(e){
        let { opinionForm } = this.data;
        let { item } = e.currentTarget.dataset;
        opinionForm.id = item.id;
        opinionForm.expName = item.expName;
        opinionForm.expPhone = item.expPhone;
        opinionForm.expDept = item.expDept;
        this.setData({
            currentItem: item,
            showOpinion: true,
            opinionForm
        })
    },
    /** 提交意见输入框输入时 */
    opinionInput(e){
        let key = e.target.dataset.key;
        this.setData({
            ['opinionForm.'+key]: e.detail.value
        })   
    },

    /** 提交意见选择框变化时 */
    opinionPickerChagne(e){
        let { key, value } = e.detail;
        this.setData({
            // array[e.detail.value].approvCode + (array[e.detail.value].returnStepId || '');
            ['opinionForm.'+key]: value
        })
    },
    /** 提交意见取消 */
    cancelOpinion(){
        let { opinionForm } = this.data;
        Object.keys(opinionForm).forEach((key) => (opinionForm[key] = ''))
        this.setData({
            showOpinion: false,
            currentItem: null,
            opinionForm
        })
    },
    /** 提交意见确认 */
    confirmOpinion(){
        let { opinionForm } = this.data;
        //表单校验
        if (!opinionForm.state) {
            wx.showToast({ title: "请选择意见!", icon: "none" });
            return
        }
        if (opinionForm.state == '3' && opinionForm.remark == '') {//如果选择了拒件，拒件理由为必填
            wx.showToast({ title: "请输入备注!", icon: "none" });
            return;
        }
        this.setData({
            showOpinion: false
        })
        request({
            url: `/experienceUser/approveExperienceUser`,
            // method: "GET",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: false, // 返回值是否解密
            data: opinionForm
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                Object.keys(opinionForm).forEach((key) => {
                    　opinionForm[key] = ""
                })
                this.setData({
                    currentItem: null,
                    opinionForm
                })
                this.refresh()
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /** 提交待办任务 */
    submitTask(){
        let { currentItem, opinionForm } = this.data;
        request({
            url: `/businessFlow/submitTask`,
            // method: "GET",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: false, // 返回值是否解密
            data: {
                versionId: currentItem.versionId,
                instanceId: currentItem.instanceId,
                stepId: currentItem.stepId,
                taskId: currentItem.taskId,
                keyInfo: currentItem.keyInfo,
                approveType: opinionForm.approveType,
                approveMemo: opinionForm.approveMemo,
            }
        }).then(res => {
            if(res.code == 0){
                console.log(res)
                Object.keys(opinionForm).forEach((key) => {
                    　opinionForm[key] = ""
                })
                this.setData({
                    currentItem: null,
                    opinionForm
                })
                this.refresh()
            }
        }).catch(error => {
            // console.log(error)
        })
    },
    /** 跳转审批记录 */
    toApprovalrecord(e) {
        let { item } = e.currentTarget.dataset;
        console.log(item)
        wx.navigateTo({
          url: `/manager/pages/approvalrecord/index?instanceId=${item.instanceId}`,
        })
    },
})